import { ApiRes, User, UserProfile } from "@/types/data";
import { RootThunkAction } from "@/types/store";
import { http } from "@/utils/http";

// 获得用户自己的信息
export const getUser = (): RootThunkAction => {
    return async (dispatch) => {
        const res = await http.get<ApiRes<User>>("/user");
        const { data } = res.data;
        dispatch({
            type: "profile/getUser",
            payload: data,
        });
    };
};
// 获取用户个人资料
export const getUserProfile = (): RootThunkAction => {
    return async (dispatch) => {
        const res = await http.get<ApiRes<UserProfile>>("/user/profile");
        dispatch({
            type: "profile/getUserProfile",
            payload: res.data.data,
        });
    };
};
// 修改资料
export const updateUserProfile = (
    userProfile: Partial<UserProfile>
): RootThunkAction => {
    return async (dispatch) => {
        await http.patch("/user/profile", userProfile);
        dispatch({
            type: "profile/update",
            payload: userProfile,
        });
    };
};
// 修改头像
export const updateUserPhoto = (data: FormData): RootThunkAction => {
    return async (dispatch) => {
        const res = await http.patch<ApiRes<{ photo: string }>>(
            "/user/photo",
            data
        );
        dispatch({
            type: "profile/update",
            payload: {
                photo: res.data.data.photo,
            },
        });
    };
};
